<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工编号" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入员工编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="允许标志" prop="authFlag">
        <el-select v-model="queryParams.authFlag" placeholder="请选择允许标志" clearable size="small" style="width: 240px">
          <el-option
              v-for="dict in authFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:finereport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:finereport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:finereport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:finereport:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="finereportList" @selection-change="handleSelectionChange" :default-sort="{ prop: 'fieldSort', order: 'descending' }" @sort-change="tableSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id"  :sort-orders="['ascending', 'descending']" sortable="custom"/> -->
      <!-- <el-table-column label="系统名称" align="center" prop="systemName" /> -->
      <el-table-column label="员工编号" align="center" prop="userNo"  :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="姓名" align="center" prop="nickName"  :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <!-- <el-table-column label="密码" align="center" prop="passWord" /> -->
      <el-table-column label="手机号" align="center" prop="mobile"  :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="邮箱" align="center" prop="email"  :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="账号状态" align="center" prop="authFlag" :sort-orders="['ascending', 'descending']" sortable="custom">
       <template slot-scope="scope">
          <el-switch
            v-if="scope.row.systemName"
            v-model="scope.row.authFlag"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="启用日期" align="center" prop="openTime" :sort-orders="['ascending', 'descending']"
            sortable="custom"/>
      <el-table-column label="禁用日期" align="center" prop="cancelTime" :sort-orders="['ascending', 'descending']"
            sortable="custom"/>
      <el-table-column label="备注" prop="remark" :sort-orders="['ascending', 'descending']"
            sortable="custom"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:finereport:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:finereport:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改帆软用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工编号" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入员工编号" />
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="form.passWord" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="账号状态" prop="authFlag">
          <el-select v-model="form.authFlag" placeholder="请选择账号状态" clearable size="small" style="width: 240px">
          <el-option
              v-for="dict in authFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
        </el-select>
        </el-form-item>
         <el-form-item label="启用日期" prop="openTime" >
           <el-date-picker
            v-model="form.openTime"
            :disabled="form.userStatus=='删除'"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择启用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="禁用日期" prop="cancelTime">
          <el-date-picker
            v-model="form.cancelTime"
            :disabled="form.userStatus=='删除'"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择禁用日期">
          </el-date-picker>
        </el-form-item>
         <el-form-item label="备注" prop="remark" >
          <el-input type="textarea" v-model="form.remark" :disabled="form.userStatus=='删除'"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFinereport, getFinereport, delFinereport, addFinereport, updateFinereport, exportFinereport } from "@/api/system/finereport";

export default {
  name: "Finereport",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组名称
      nickNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帆软用户信息表格数据
      finereportList: [],
      //
      authFlagOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemName: null,
        userNo: null,
        nickName: null,
        passWord: null,
        mobile: null,
        email: null,
        authFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userNo: [
          { required: true, message: "员工编号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_hr_status").then(response => {
      this.authFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询帆软用户信息列表 */
    getList() {
      this.loading = true;
      listFinereport(this.queryParams).then(response => {
        this.finereportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        systemName: null,
        userNo: null,
        nickName: null,
        passWord: null,
        mobile: null,
        email: null,
        authFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nickNames = selection.map(item => item.nickName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加帆软用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFinereport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改帆软用户信息";
      });
    },
     /**修改用户允许状态 */
    handleStatusChange(row) {
      let text = row.authFlag === "0" ? "允许" : "不允许";
      const data = {
        id: row.id,
        authFlag: row.authFlag
      };
      this.$confirm(
        '确认要"' + text + '""' + row.nickName + '"使用单点登陆吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
           updateFinereport(data);
        })
        .then(() => {
          this.msgSuccess("修改成功");
          this.getList();
        })
        .catch(function () {
          row.authFlag = row.authFlag === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFinereport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFinereport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nickNames = row.nickName || this.nickNames;
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除帆软用户信息为"' + nickNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFinereport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有帆软用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFinereport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
  }
};
</script>
